package xonmp.alarmmanager;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AlarmManagerActivity extends Activity {
	
	private Button mStartBtn1, mStartBtn2, mStartBtn3, mStartBtn4;
	private EditText mTxtSeconds;
	private Toast mToast;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
//        mStartBtn1 = (Button) findViewById(R.id.btnSetAlarm1);
        mStartBtn2 = (Button) findViewById(R.id.btnSetAlarm2);
        mStartBtn3 = (Button) findViewById(R.id.btnSetAlarm3);
        mStartBtn4 = (Button) findViewById(R.id.btnSetAlarm4);
        mTxtSeconds = (EditText) findViewById(R.id.txtSeconds);
        
//        mStartBtn1.setOnClickListener(new OnClickListener() {
//
//			public void onClick(View view) {
//				try
//				{
//					int i = Integer.parseInt(mTxtSeconds.getText().toString());
//					Intent intent = new Intent(AlarmManagerActivity.this, AlarmBroadcastReceiver.class);
//					PendingIntent pendingIntent = 
//							PendingIntent.getBroadcast(AlarmManagerActivity.this, 
//							1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//					AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
//					am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
//					
//					if (mToast != null)
//					{
//						mToast.cancel();
//					}
//					
//					mToast = Toast.makeText(AlarmManagerActivity.this, 
//							"Alarm for broadcast receiver is set in: " + i + " seconds", 
//							Toast.LENGTH_LONG);
//					mToast.show();
//				} catch (NumberFormatException e)
//				{
//					if (mToast != null)
//					{
//						mToast.cancel();
//					}
//					mToast = Toast.makeText(AlarmManagerActivity.this, 
//								"Please enter some number in the text field and try again!", 
//								Toast.LENGTH_LONG);
//					mToast.show();
//					Log.i("AlarmManagerActivity", "Number format exception");
//				}
//				
//			}
//        	
//        });
        
        mStartBtn2.setOnClickListener(new OnClickListener() {
        	public void onClick(View view) {
        		try
        		{
					int i = Integer.parseInt(mTxtSeconds.getText().toString());
					Intent intent = new Intent(AlarmManagerActivity.this, AlarmReceiverActivity.class);
					PendingIntent pendingIntent = 
							PendingIntent.getActivity(AlarmManagerActivity.this, 2, intent, 
			        				PendingIntent.FLAG_CANCEL_CURRENT);
					AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
					am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
					
					if (mToast != null)
					{
						mToast.cancel();
					}
					
					mToast = Toast.makeText(getApplicationContext(), 
							"Alarm for activity is set in: " + i + " seconds", 
							Toast.LENGTH_LONG);
					mToast.show();
        		} catch (NumberFormatException e)
				{
					if (mToast != null)
					{
						mToast.cancel();
					}
					mToast = Toast.makeText(AlarmManagerActivity.this, 
								"Please enter some number in the text field and try again!", 
								Toast.LENGTH_LONG);
					mToast.show();
					Log.i("AlarmManagerActivity", "Number format exception");
				}
			}
        });
        
        mStartBtn3.setOnClickListener(new OnClickListener() {
        	public void onClick(View view) {
        		try
        		{
					int i = Integer.parseInt(mTxtSeconds.getText().toString());
					Intent intent = new Intent(AlarmManagerActivity.this, RepeatingAlarmReceiverActivity.class);
					PendingIntent pendingIntent = 
							PendingIntent.getActivity(AlarmManagerActivity.this, 3, intent, 
			        				PendingIntent.FLAG_CANCEL_CURRENT);
					AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
					am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000),
							15 * 1000, pendingIntent);
					
					if (mToast != null)
					{
						mToast.cancel();
					}
					
					mToast = Toast.makeText(getApplicationContext(), 
							"Repeating alarm for activity is set in: " + i + " seconds,"
							+ " and repeat every 15 seconds after that", 
							Toast.LENGTH_LONG);
					mToast.show();
        		} catch (NumberFormatException e)
				{
					if (mToast != null)
					{
						mToast.cancel();
					}
					mToast = Toast.makeText(AlarmManagerActivity.this, 
								"Please enter some number in the text field and try again!", 
								Toast.LENGTH_LONG);
					mToast.show();
					Log.i("AlarmManagerActivity", "Number format exception");
				}
			}
        });
        
        mStartBtn4.setOnClickListener(new OnClickListener() {
        	public void onClick(View view) {
        		try
        		{
					Intent intent = new Intent(AlarmManagerActivity.this, RepeatingAlarmReceiverActivity.class);
					PendingIntent pendingIntent = 
							PendingIntent.getActivity(AlarmManagerActivity.this, 3, intent, 
			        				0);
					AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
					am.cancel(pendingIntent);
					
					if (mToast != null)
					{
						mToast.cancel();
					}
					
					mToast = Toast.makeText(getApplicationContext(), 
							"Repeating alarm has been cancelled!", 
							Toast.LENGTH_LONG);
					mToast.show();
        		} catch (NumberFormatException e)
				{
					if (mToast != null)
					{
						mToast.cancel();
					}
					mToast = Toast.makeText(AlarmManagerActivity.this, 
								"Please enter some number in the text field and try again!", 
								Toast.LENGTH_LONG);
					mToast.show();
					Log.i("AlarmManagerActivity", "Number format exception");
				}
			}
        });
        
        //Create an offset from the current time in which
        //the alarm will go off
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.SECOND, 5);
//        
//        Intent intent = new Intent(this, AlarmReceiverActivity.class);
//        PendingIntent pendingIntent = 
//        		PendingIntent.getActivity(this, 12345, intent, 
//        				PendingIntent.FLAG_CANCEL_CURRENT);
//        AlarmManager am = 
//        		(AlarmManager) getSystemService(Activity.ALARM_SERVICE);
//        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
    }
}